package com.school.sports.controller;


import com.school.sports.entity.Venue;
import com.school.sports.service.VenueService;
import com.school.sports.common.Result;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("/venues")
public class VenueController {

    @Autowired
    private VenueService venueService;

    /**
     * 新增场地
     */
    @PostMapping
    public Result<String> addVenue(@Valid @RequestBody Venue venue) {
        //简单校验，确保名称和位置不为空(校验注解已在实体类中定义)

        // 保存并返回结果
        boolean success = venueService.save(venue);
        return success ? Result.success("新增成功") : Result.fail("新增失败，场地名称可能已存在");
    }

    /**
     * 查询所有场地
     */
    @GetMapping
    public Result<List<Venue>> getAllVenues() {
        List<Venue> venues = venueService.list();
        return Result.success(venues);
    }

    /**
     * 根据ID查询场地
     */
    @GetMapping("/{id}")
    public Result<Venue> getVenueById(@PathVariable(name = "id") Integer id) {
        Venue venue = venueService.getById(id);
        if (venue == null) {
            return Result.fail("未找到ID为" + id + "的场地");
        }
        return Result.success(venue);
    }

    /**
     * 修改场地信息
     */
    @PutMapping
    public Result<String> updateVenue(@RequestBody Venue venue) {
        if (!venueService.existsById(venue.getId())) {
            return Result.fail("未找到ID为" + venue.getId() + "的场地，无法更新");
        }
        venueService.updateById(venue);
        return Result.success("场地更新成功");
    }

    /**
     * 删除场地
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteVenue(@PathVariable(name = "id") Integer id) {
        if (!venueService.existsById(id)) {
            return Result.fail("未找到ID为" + id + "的场地，无法删除");
        }
        venueService.removeById(id);
        return Result.success("场地删除成功");
    }

    /**
     * 按状态查询场地
     */
    @GetMapping("/byStatus")
    public Result<List<Venue>> getVenuesByStatus(@RequestParam(name = "status") String status) {
        List<Venue> venues = venueService.getByStatus(status);
        return Result.success(venues);
    }

}